<?php

namespace app\common\model;

use think\Model;
use think\Loader;
use Carbon\Carbon;
use think\Request;
use think\Exception;
use think\Lang;
use app\common\traits\model\TraitModel;

class Notices extends Model
{
	use TraitModel;

    protected $resultSetType = 'collection';

    protected $table = 'notices';

	const READ_STATUS = 1;
	const NOT_READ_STATUS = 0;

    public function messages()
    {
    	return $this->belongsTo('Messages');//->field('id,content');
    }

    protected function getRecipients()
    {
        return Request::instance()->user->sign;
    }

    /**
     * 发送通知信息
     * @param  [type] $title      [description]
     * @param  [type] $content    [description]
     * @param  [type] $recipients [description]
     * @return [type]             [description]
     */
    public function sendNotice($sender, $title, $content, $recipients)
    {
        $msg_info = [
            'sender'     => $sender ? : 0,
            'title'      => $title,
            'content'    => $content ? : $title,
            'recipients' => $recipients
        ];
        Loader::model('Messages')->createMsg($msg_info);
    }

    /**
     * 获得通知条数
     * @param  [type] $reading [description]
     * @return [type]         [description]
     */
    public function getNoticeCount($reading = self::NOT_READ_STATUS)
    {
        return $this->where(['reading' => $reading, 'recipients' => $this->getRecipients()])->count();
    }

    /**
     * 获取通知信息列表
     * @param  [type] $status [description]
     * @return [type]         [description]
     */
    public function getNoticeList($reading = self::NOT_READ_STATUS)
    {

        return $this->getInfoList(['recipients' => $this->getRecipients()], 'messages');
    }

    /**
     * 获取信息
     * @param  [type] $msg_id [description]
     * @return [type]         [description]
     */
    public function getNotice($id)
    {
        $msg = $this->get(['id' => $id, 'recipients' => $this->getRecipients()], 'messages');
        if (!$msg) {
            throw new Exception(Lang::get("Notification information does not exist"), 30001);
        }
	    return $msg;
    }

    public function readNotice($id)
    {
        $notice_info = $this->getNotice($id);

        if ($notice_info->reading == self::NOT_READ_STATUS) {
            
            $notice_info->reading   = self::READ_STATUS;
            $notice_info->read_time = Carbon::now()->toDateTimeString();

            $notice_info->allowField(true)->save();
        }

        return $notice_info->messages;
    }

    /**
     * 设置所有未读信息为已读
     */
    public function setAllNoticesIsRead()
    {
        $update_info = [
            'reading'   => self::READ_STATUS, 
            'read_time' => Carbon::now()->toDateTimeString()
        ];

        $update_where = [
            'recipients' => $this->getRecipients(),
            'reading'    => self::NOT_READ_STATUS, 
        ];

        $this->update($update_info, $update_where, true);

        return true;
    }

    /**
     * 删除信息
     * @param  [type] $msg_id [description]
     * @return [type]         [description]
     */
    public function delNotice($id)
    {
        $where = is_array($id) ? ['id' => ['in', $id]] : ['id' => $id];
        $where = array_merge($where, ['recipients' => $this->getRecipients()]);
		return self::destroy($where);
    }

}
